package com.mcxx.modules.system.service.write;

import com.mcxx.modules.system.dao.read.PolicyFileReadDao;
import com.mcxx.modules.system.dao.write.PolicyFileWriteDao;
import com.mcxx.modules.system.dao.write.PolicyWriteDao;
import com.mcxx.modules.system.dto.PolicyDTO;
import com.mcxx.modules.system.dto.PolicyFileDTO;
import com.mcxx.modules.system.entity.PolicyFileEntity;
import com.mcxx.result.BaseResult;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service("commonPolicyWriteService")
@TaTransactional
public class PolicyWriteService {
    @Autowired
    private PolicyWriteDao commonPolicyWriteDao;
    @Autowired
    private PolicyFileWriteDao commonPolicyFileWriteDao;
    @Autowired
    private PolicyFileReadDao commonPolicyFileReadDao;

    public static String preFFux ="D";
    public static SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");


    public BaseResult<Boolean> deletePolicy(List<String> policyIdList){
        BaseResult<Boolean> rstMsg = new BaseResult<>();
        StringBuffer msg = new StringBuffer();











        commonPolicyFileWriteDao.deletes(policyIdList);

        commonPolicyWriteDao.deletes(policyIdList);

        rstMsg.setMsg("删除成功");
        return rstMsg;
    }


    public BaseResult<Boolean> addPolicy(PolicyDTO policy, List<PolicyFileDTO> policyFileList, UserAccountVo userAccount) {
        BaseResult<Boolean> result = new BaseResult<>();

        String dateString = formatter.format(new Date());

        policy.setId(preFFux+dateString);

        policy.setOrgId(userAccount.getOrgId());


        commonPolicyWriteDao.insert(policy);


        if(CollectionUtils.isNotEmpty(policyFileList)){
            policyFileList.forEach(policyFile->{
                policyFile.setId(Constant.getUUID());
                policyFile.setPolicyId(policy.getId());
                policyFile.setCreateTime(new Date());
            });

            commonPolicyFileWriteDao.inserts(policyFileList);
            result.setMsg("新增成功");
        }
        return result;
    }


    public BaseResult<Boolean> updatePolicy(PolicyDTO policy, List<PolicyFileDTO> policyFileList) {
        BaseResult<Boolean> result = new BaseResult<>();

        commonPolicyWriteDao.update(policy);


        if(CollectionUtils.isNotEmpty(policyFileList)){
            policyFileList.forEach(policyFile->{
                policyFile.setId(Constant.getUUID());
                policyFile.setPolicyId(policy.getId());
                policyFile.setCreateTime(new Date());
            });

            commonPolicyFileWriteDao.inserts(policyFileList);
            result.setMsg("修改成功");
        }
        return result;
    }


    public BaseResult<Boolean>  deletePolicyFile(String policyFileId) {
        BaseResult<Boolean> result  = new BaseResult<>();
        try{

            boolean rst = commonPolicyFileWriteDao.deleteById(policyFileId);
            result.setMsg(rst ? "删除成功" : "删除失败");
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return result;
    }

}
